package com.example.rocketmqdemo.transaction.app;

import com.alibaba.cola.exception.BizException;
import com.alibaba.cola.exception.SysException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Component
public class TransferCmdExecutor {

    @Transactional(rollbackFor = Exception.class)
    public void execute(String from, String to, BigDecimal amount) {
        log.info("from: {}, to: {}, amount: {}", from, to, amount);
        if (amount.intValue() <= 10) {
            throw new RuntimeException("未知错误，比如JDBC连接失败等");
        }
        if (amount.intValue() <= 20) {
            throw new BizException("业务异常，余额不足");
        }
        if (amount.intValue() <= 30) {
            throw new SysException("系统错误，提前知道并处理的错误，比如文件不存等");
        }

    }
}
